<template>
	<div>
		<div style="float: right;margin-right:2%;">
			<el-button style='margin: 10px 0;' size="mini" type="primary"
				icon="document" @click="sxNotice">刷新</el-button>
			<el-button style='margin: 10px 0;' :disabled="!isAuth('IntegralGoods:add')" size="mini" type="primary"
				icon="document" @click="addNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="tableData.records">

			<el-table-column fixed prop="rechargeId" label="编号" width="50">
			</el-table-column>
			<!-- <el-table-column prop="memberImg" label="图片">
        <template slot-scope="scope">
          　　<img :src="scope.row.memberImg" width="60" height="60" />
        </template>
      </el-table-column> -->
			<el-table-column prop="money" label="充值金额">
			</el-table-column>
			<el-table-column prop="couponNum" label="赠送优惠券数量">
			</el-table-column>
			
			<el-table-column prop="tbCoupon.couponName" label="赠送优惠券">
			</el-table-column>
			<el-table-column prop="sort" label="排序">
			</el-table-column>
			<el-table-column prop="isEnable" label="是否启用">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.isEnable" @change="change2(scope.row.isEnable,scope.row)"
						:active-value="openValue" :inactive-value="closeValue" active-color="#13ce66"
						inactive-color="#ff4949">
					</el-switch>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('IntegralGoods:update')"
						@click="amendBanner(scope.$index, scope.row)">修改
					</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('IntegralGoods:delete')"
						@click="deletes(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[5, 10, 20, 30, 50, 100]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="tableData.total">
			</el-pagination>
		</div>
		<el-dialog :title="title" :visible.sync="dialogFormVisible" center>
			<!-- <div style="margin-bottom: 10px;display: flex;">
    		<span style="width: 200px;display: inline-block;text-align: right;">图片：</span>
    		<div style=" width:148px;height:148px;border: 1px dashed #c0ccda;border-radius: 6px;text-align: center;line-height: 148px;">
    			<el-upload
    				class="avatar-uploader"
    				v-model="memberImg"
    				:action="$http.adornUrlUp('alioss/upload')"
    				:show-file-list="false"
    				:on-success="handleAvatarSuccess"
    				>
    				<img v-if="memberImg" :src="memberImg" class="avatar" style="border-radius: 6px;width: 148px;height: 148px;"/>
    				<i v-else class="el-icon-plus avatar-uploader-icon"></i>
    			</el-upload>
    		</div>
    	</div> -->
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">充值金额：</span>
				<el-input style="width:50%;" v-model="money" placeholder="请输入充值金额"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">赠送优惠券数量：</span>
				<el-input style="width:50%;" v-model="giveNum" placeholder="请输入赠送优惠券数量"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">赠送的优惠券：</span>
				<!-- <el-button size="small" v-if="couponName!=''">{{couponName}}</el-button> -->
				<span v-if="couponName!=''">
					<el-tag v-for="tag in couponName.split(',')" :key="tag" closable @close="closes()" :type="tag">
						{{tag}}
					</el-tag>
				</span>
			
				<!-- <el-input style="width:50%;" v-model="couponName" placeholder="请输入赠送的优惠券" disabled></el-input> -->
				<el-button type="primary" plain class="button-new-tag" size="small" @click="couponIdBtn()">选择优惠券
				</el-button>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">排序：</span>
				<el-input style="width:50%;" v-model="sort" placeholder="请输入排序"
					onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 优惠券列表 -->
		<el-dialog title="优惠券列表" :visible.sync="dialogFormVisible4" center>
		
			<el-table v-loading="tableDataLoading" :data="tableDataY.records">
				<el-table-column prop="couponId" label="编号">
				</el-table-column>
				<el-table-column prop="couponName" label="优惠券名称">
				</el-table-column>
				<el-table-column prop="money" label="优惠券面值" width="100">
				</el-table-column>
				<el-table-column prop="minMoney" label="优惠券最低消费" width="150">
				</el-table-column>
				<el-table-column prop="validDays" label="优惠券有效期限(天)" width="150">
					<template slot-scope="scope">
						<span>{{scope.row.validDays==0?'永久':scope.row.validDays}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="操作">
					<template slot-scope="scope">
						<el-button class="button-new-tag" size="small" @click="couponIdBtnQ(scope.row)">添加</el-button>
					</template>
				</el-table-column>
		
			</el-table>
			<div style="text-align: center;margin-top: 10px;">
				<el-pagination @size-change="handleSizeChangeY" @current-change="handleCurrentChangeY"
					:page-sizes="[10, 20, 30, 50, 100]" :page-size="size1" :current-page="page1"
					layout="total,sizes, prev, pager, next" :total="tableDataY.total">
				</el-pagination>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 10,
				tableDataLoading: true,
				dialogFormVisible: false,
				tableData: [],
				redPacketMoney: '',
				memberImg: '',
				isEnable: 1,
				money: '',
				redPacketNum: '',
				rechargeId: '',
				title: '添加',
				openValue: 1,
				closeValue: 0,
				tableDataY: {},
				price: '',
				memberId: '',
				giveNum: '',
				couponId: '',
				couponName: '',
				sort: '',
				payClassifyId: '',
				dialogFormVisible4: false,
				size1:10,
				page1:1,
				
			}
		},
		methods: {
			// 获取数据列表
			dataSelect() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/rechargeConfig/getConfigInfoList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit
					})
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log('会员列表', data)
						this.tableDataLoading = false
						let returnData = data.data;
						this.tableData = returnData;
						if(this.tableData.records.length==0){
							if(this.page>1){
								this.page = this.page-1
								this.dataSelect()
							}
						}
					}

				})
			},
			//上传成功
			handleAvatarSuccess(file) {
				this.memberImg = file.data
			},
			// 刷新
			sxNotice(){
				this.page = 1
				this.dataSelect()
			},
			addNotice() {
				this.money = ''
				this.rechargeId = ''
				this.redPacketMoney = ''
				this.redPacketNum = ''
				this.sort = ''
				this.isEnable = 1
				this.couponId = ''
				this.couponName = ''
				this.giveNum = ''
				this.title = '添加'
				this.dialogFormVisible = true
			},
			handleCurrentChange(val) {
				this.page = val
				this.dataSelect()
			},
			handleSizeChange(val) {
				this.limit = val
				this.dataSelect()
			},
			handleCurrentChangeY(val) {
				this.page1 = val
				this.dataSelectY()
			},
			handleSizeChangeY(val) {
				this.size1 = val
				this.dataSelectY()
			},
			// 添加
			addNoticeTo() {
				if (this.money == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入充值金额',
						type: 'warning'
					});
					return
				}
				if(this.couponId != ''||this.giveNum !== ''){
					if (this.couponId === '') {
						this.$notify({
							title: '提示',
							duration: 1800,
							message: '请选择赠送的优惠券',
							type: 'warning'
						});
						return
					}
					if (this.giveNum == '') {
						this.$notify({
							title: '提示',
							duration: 1800,
							message: '请输入赠送数量',
							type: 'warning'
						});
						return
					}
				}
				
				this.classify = 1
				if (this.title == '添加') {
					var url = 'admin/rechargeConfig/addConfig'
				} else {
					var url = 'admin/rechargeConfig/updateConfig'
				}
				this.$http({
					url: this.$http.adornUrl(url),
					method: 'post',
					params: this.$http.adornParams({
					// data: this.$http.adornData({
						'money': this.money,
						'isEnable': this.isEnable,
						// 'redPacketMoney': this.redPacketMoney,
						'sort': this.sort,
						'couponNum': this.giveNum,
						'couponId':this.couponId,
						'rechargeId': this.rechargeId,
					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.dialogFormVisible = false
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								
								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
							}
						})
					}
					
				})
			},
			// 修改
			amendBanner(index, rows, a) {
				if (a == 1) {
					this.pl = 1
				} else {
					this.pl = 0
				}
				this.dialogFormVisible = true
				this.rechargeId = rows.rechargeId
				this.money = rows.money
				this.sort = rows.sort
				this.redPacketNum = rows.redPacketNum
				// this.redPacketMoney = rows.redPacketMoney
				this.isEnable = rows.isEnable
				this.title = '修改'
				this.dialogFormVisible = true
				// this.giveNum = rows.couponNum
				this.sort = rows.sort
				this.couponId = rows.couponId
				if (rows.couponId) {
					this.couponId = rows.couponId
				} else {
					this.couponId = ''
				}
				if (rows.tbCoupon) {
					this.couponName = rows.tbCoupon.couponName
				} else {
					this.couponName = ''
				}
				if (rows.couponNum) {
					this.giveNum = rows.couponNum
				} else {
					this.giveNum = ''
				}
				
				this.price = rows.money
			},
			//删除一级
			deletes(row) {
				let delid = row.rechargeId
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: ''
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('admin/rechargeConfig/deleteConfig'),
						method: 'get',
						params: this.$http.adornParams({
							'rechargeId':delid
						})
					}).then(({
						data
					}) => {
						if(data.code==0){
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.dataSelect()
								}
							})
						}else{
							this.$message({
								message: data.msg,
								type: 'warning',
								duration: 1500,
								onClose: () => {
								}
							})
						}
						
					})
				}).catch(() => {})
			},
			// 是否启用
			change2(val, row) {
				this.$http({
					url: this.$http.adornUrl('admin/rechargeConfig/updateConfig'),
					method: 'post',
					params: this.$http.adornParams({
					// data: this.$http.adornData({
						'money': row.money,
						'isEnable': row.isEnable,
						'redPacketMoney': row.redPacketMoney,
						'sort': row.sort,
						'redPacketNum': row.redPacketNum,
						'rechargeId': row.rechargeId,
					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}
				})
			},
			//获取优惠券数据
			dataSelectY() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/coupon/getCouponPageList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page1,
						'limit': this.size1
					})
				}).then(({
					data
				}) => {
					this.tableDataLoading = false
					let returnData = data.data;
					this.tableDataY = returnData
				})
			},
			// 打开优惠券列表
			couponIdBtn() {
				this.dataSelectY()
				this.dialogFormVisible4 = true
			},
			couponIdBtnQ(rows) {
				this.couponName = rows.couponName
				this.couponId = rows.couponId
				this.dialogFormVisible4 = false
			},
			closes() {
				this.couponName = ''
				this.couponId = ''
			}
		},
		mounted() {
			this.dataSelect()
		}
	}
</script>

<style>
</style>
